package com.itzyf.pokemondata.activity

import android.view.inputmethod.EditorInfo
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.itzyf.pokemondata.R
import com.itzyf.pokemondata.entity.PageVo
import com.itzyf.pokemondata.entity.Price
import com.itzyf.pokemondata.entity.ResultVo
import com.itzyf.pokemondata.service.PokemonService
import com.itzyf.pokemondata.utils.OnRefreshListener
import com.itzyf.pokemondata.utils.RetrofitUtils
import com.itzyf.pokemondata.utils.setOnRefreshListener
import com.itzyf.pokemondata.utils.subZeroAndDot
import com.tydic.baselibrary.activity.BaseActivity
import kotlinx.android.synthetic.main.activity_list.*
import retrofit2.Call
import retrofit2.Response
import java.text.SimpleDateFormat
import java.util.*

class PriceActivity : BaseActivity() {
    var mDatas: MutableList<Price?> = ArrayList()
    var index = 0

    private lateinit var mAdapter: QuickAdapter

    override fun getContentViewId() = R.layout.activity_list

    override fun getToolbarTitle() = "精灵价格表"

    override fun handlerActivity() {

        mAdapter = QuickAdapter(R.layout.item_text_data2, mDatas)
        rv_list.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(this)
        rv_list.adapter = mAdapter
        showProgressDialog()
        queryDatas()

        mAdapter.setOnLoadMoreListener({ queryDatas(index++) }, rv_list)

        pull_to_refresh.setOnRefreshListener(object : OnRefreshListener {
            override fun onRefresh() {
                index = 0
                queryDatas(index)
            }
        })

        mBtnSearch.setOnClickListener {
            val query = mSearchView.text.toString()
            queryDatas(query = query)
        }
        mSearchView.setOnEditorActionListener(TextView.OnEditorActionListener { _, actionId, _ ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                val query = mSearchView.text.toString()
                queryDatas(query = query)
                return@OnEditorActionListener true
            }
            false
        })


    }

    fun queryDatas(index: Int = 0, pageSize: Int = 20, query: String = "") {
        showProgressDialog()
        RetrofitUtils.getInstance()
            .getApiService(PokemonService::class.java)
            .queryListPrice(index + 1, pageSize, query)
            .enqueue(object : retrofit2.Callback<ResultVo<PageVo<Price>>> {
                override fun onFailure(call: Call<ResultVo<PageVo<Price>>>, t: Throwable) {
                    dismissProgressDialog()
                }

                override fun onResponse(
                    call: Call<ResultVo<PageVo<Price>>>,
                    response: Response<ResultVo<PageVo<Price>>>
                ) {
                    dismissProgressDialog()
                    val body = response.body()
                    if (body!!.code == 200) {
                        if (index == 0) {
                            mDatas.clear()
                            pull_to_refresh.finishRefresh()
                        }
                        mDatas.addAll(body.result.records)

                        if (body.result.pages <= index + 1) {
                            mAdapter.loadMoreEnd()
                        } else {
                            mAdapter.loadMoreComplete()
                        }
                        mAdapter.notifyDataSetChanged()
                    }

                }
            })
    }

    internal inner class QuickAdapter(layoutResId: Int, data: List<Price?>) :
        BaseQuickAdapter<Price, BaseViewHolder>(layoutResId, data) {

        override fun convert(helper: BaseViewHolder, item: Price?) {
            item?.let {
                helper.setImageResource(R.id.icon, R.drawable.ic_new)
                helper.setGone(R.id.icon, it.isTop)
                helper.setText(R.id.tvDiscount, "${it.price}")
                helper.setText(R.id.text, it.name)
            }

        }
    }
}
